package com.pks.datacenter.service.basic;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.pks.datacenter.entity.basic.DbInfo;
import com.pks.datacenter.entity.basic.JobInfo;
import com.pks.datacenter.job.QuartzJob;
import com.pks.datacenter.mapper.basic.DbFieldMapper;
import com.pks.datacenter.mapper.basic.DbInfoMapper;
import com.pks.datacenter.mapper.basic.JobInfoMapper;
import com.pks.datacenter.utils.QuartzManager;
import com.pks.web.dto.ServiceResult;
import com.pks.web.mybatis.service.BasicServiceImpl;

@Component("jobInfoService")
public class JobInfoServiceImpl extends BasicServiceImpl<JobInfo, Long, JobInfoMapper> implements JobInfoService {

	@Autowired
	private JobInfoMapper jobInfoMapper;
	@Autowired
	private DbInfoMapper dbInfoMapper;
	@Autowired
	private DbFieldMapper dbFieldMapper;

	@Override
	protected JobInfoMapper getMapper() {
		return jobInfoMapper;
	}

	@Override
	public void deleteJob(Long id) throws Exception {
		jobInfoMapper.delete(id);
		QuartzManager.deleteJob("job_"+id);
	}

	@Override
	public ServiceResult insertOrUpdate(JobInfo obj) throws Exception {
		if(obj.getDb_source() == null || obj.getDb_target() == null){
			return ServiceResult.initError("请选择数据源和目标库");
		}
		if(StringUtils.isBlank(obj.getExec_time())){
			return ServiceResult.initError("请设置任务调度时间");
		}
		if(obj.getType() == null || (JobInfo.type_add != obj.getType() && JobInfo.type_cover != obj.getType())){
			return ServiceResult.initError("请设置正确的数据处理方式");
		}
		DbInfo source = dbInfoMapper.findById(obj.getDb_source());
		if(source == null){
			return ServiceResult.initError("数据源不存在");
		}
		DbInfo target = dbInfoMapper.findById(obj.getDb_target());
		if(target == null){
			return ServiceResult.initError("目标库不存在");
		}
		if(DbInfo.db_type_mysql != target.getDb_type() && DbInfo.db_type_oracle != target.getDb_type() && DbInfo.db_type_sqlserver != target.getDb_type()){
			return ServiceResult.initError("目前只支持目标库为数据库类型");
		}
		if(source.getField_type() != target.getField_type()){
			return ServiceResult.initError("数据源的排序自增字段和目标库的排序自增字段类型不匹配");
		}
		if(obj.getId()==null){
			obj.insertBefore();
			jobInfoMapper.insert(obj);
			if(JobInfo.state_on == obj.getState()){
				QuartzManager.addJob("job_"+obj.getId(), QuartzJob.class, obj.getExec_time(), obj.getId().toString());
			}
		}else{
			JobInfo update = jobInfoMapper.findById(obj.getId());
			if(update==null){
				return ServiceResult.initError("数据不存在");
			}
			jobInfoMapper.update(obj);
			if(JobInfo.state_on == obj.getState()){
				if(JobInfo.state_on == update.getState()){
					if(!obj.getExec_time().equals(update.getExec_time())){
						QuartzManager.modifyJob("job_"+obj.getId(), obj.getExec_time());
					}
				}else{
					QuartzManager.addJob("job_"+obj.getId(), QuartzJob.class, obj.getExec_time(), obj.getId().toString());
				}
			}else{
				QuartzManager.deleteJob("job_"+obj.getId());
			}
			
		}
		return ServiceResult.initSuccess(null);
	}
}
